package com.clever.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clever.bean.shopping.projo.output.ChatMessageDetailVO;
import com.clever.util.SpringUtil;
import com.clever.annotation.Auth;
import com.clever.annotation.AuthGroup;
import com.clever.bean.model.OnlineUser;
import com.clever.bean.model.Result;

import java.util.List;

import com.clever.bean.shopping.ChatMessage;
import com.clever.service.ChatMessageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 聊天记录表接口
 *
 * @Author xixi
 * @Date 2024-04-08 11:11:32
 */
@RestController
@Validated
@RequestMapping("/chatMessage")
@AuthGroup(value = "order-second-hand-transaction.chatMessage", name = "聊天记录表模块", description = "聊天记录表模块权限组")
public class ChatMessageController {

    @Resource
    private ChatMessageService chatMessageService;


    /**
     * 获取消息列表
     *
     * @param targetId 对象id
     * @return Result<List < ChatMessageDetailVO>>
     */
    @GetMapping("/getMessageList")
    public Result<List<ChatMessageDetailVO>> selectMessageDetailList(String targetId) {
        return new Result<>(chatMessageService.selectMessageDetailList(targetId, SpringUtil.getOnlineUser().getId()));
    }

    /**
     * 阅读消息
     *
     * @param senderId 发送者id
     * @return Result<String>
     */
    @PostMapping("/read")
    public Result<String> read(String senderId) {
        chatMessageService.read(senderId, SpringUtil.getOnlineUser());
        return Result.ofSuccess();
    }

    /**
     * 分页查询聊天记录表列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     * @param senderId   发送者
     * @param receiverId 接收者
     * @return 当前页数据
     */
    @GetMapping("/page/{pageNumber}/{pageSize}")
    @Auth(value = "order-second-hand-transaction.chatMessage.page", name = "聊天记录表分页", description = "聊天记录表分页接口")
    public Result
            <Page<ChatMessage>> selectPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize, String senderId, String receiverId) {
        return new Result<>(chatMessageService.selectPage(pageNumber, pageSize, senderId, receiverId), "分页数据查询成功");
    }

    /**
     * 根据发送者获取列表
     *
     * @param senderId 发送者
     * @return List<ChatMessage> 聊天记录表列表
     */
    @GetMapping("/listBySenderId/{senderId}")
    @Auth(value = "order-second-hand-transaction.chatMessage.listBySenderId", name = "根据发送者获取聊天记录表列表", description = "根据发送者获取聊天记录表列表接口")
    public Result
            <List<ChatMessage>> selectListBySenderId(@PathVariable("senderId") String senderId) {
        return new Result<>(chatMessageService.selectListBySenderId(senderId), "查询成功");
    }

    /**
     * 根据接收者获取列表
     *
     * @param receiverId 接收者
     * @return List<ChatMessage> 聊天记录表列表
     */
    @GetMapping("/listByReceiverId/{receiverId}")
    @Auth(value = "order-second-hand-transaction.chatMessage.listByReceiverId", name = "根据接收者获取聊天记录表列表", description = "根据接收者获取聊天记录表列表接口")
    public Result
            <List<ChatMessage>> selectListByReceiverId(@PathVariable("receiverId") String receiverId) {
        return new Result<>(chatMessageService.selectListByReceiverId(receiverId), "查询成功");
    }

    /**
     * 根据id获取聊天记录表信息
     *
     * @param id id
     * @return 聊天记录表信息
     */
    @GetMapping("/{id}")
    @Auth(value = "clever-system.chatMessage.selectById", name = "根据id获取聊天记录表信息", description = "根据id获取聊天记录表信息接口")
    public Result<ChatMessage> selectById(@PathVariable("id") String id) {
        return new Result<>(chatMessageService.selectById(id), "查询成功");
    }

    /**
     * 创建聊天记录表信息
     *
     * @param chatMessage 聊天记录表实体信息
     * @return 创建后的聊天记录表信息
     */
    @PostMapping("")
    @Auth(value = "order-second-hand-transaction.chatMessage.create", name = "创建聊天记录表", description = "创建聊天记录表信息接口")
    public Result<ChatMessage> create(@Validated ChatMessage chatMessage) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(chatMessageService.create(chatMessage, onlineUser), "创建成功");
    }

    /**
     * 修改聊天记录表信息
     *
     * @param chatMessage 聊天记录表实体信息
     * @return 修改后的聊天记录表信息
     */
    @PatchMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.chatMessage.update", name = "修改聊天记录表", description = "修改聊天记录表信息接口")
    public Result<ChatMessage> update(@Validated ChatMessage chatMessage, @PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        chatMessage.setId(id);
        return new Result<>(chatMessageService.update(chatMessage, onlineUser), "修改成功");
    }

    /**
     * 保存聊天记录表信息
     *
     * @param chatMessage 聊天记录表实体信息
     * @return 保存后的聊天记录表信息
     */
    @PostMapping("/save")
    @Auth(value = "order-second-hand-transaction.chatMessage.save", name = "保存聊天记录表", description = "保存聊天记录表信息接口")
    public Result<ChatMessage> save(@Validated ChatMessage chatMessage) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(chatMessageService.save(chatMessage, onlineUser), "保存成功");
    }

    /**
     * 根据聊天记录表id删除聊天记录表信息
     *
     * @param id id
     */
    @DeleteMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.chatMessage.delete", name = "删除聊天记录表", description = "删除聊天记录表信息接口")
    public Result<String> delete(@PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        chatMessageService.delete(id, onlineUser);
        return Result.ofSuccess("删除成功");
    }
}
